import Vue from "vue";
import Vuex from "vuex";
import myaxios from "../plugins/axios";
import _ from "lodash";
Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        token: null,
        path: null, // 去到注册前的路径
        loginuser: null, // 用户信息
        catList: [], // 存储加入购物车的信息
        sitegood: [], // 存储地址
        dropShipping: [], // 待发货
        collect: [], // 收藏商品
        money: 1867, // 金额
        lodigChange: "1", // 是否显示加载logo
    },
    getters: {
        // 当浏览器刷新时,获取token
        mytoken(state) {
            if (state.token) return state.token;
            let tokens = localStorage.getItem("token");
            state.token = tokens;
            return tokens;
        },
        // 当浏览器刷新,获取mypath(防止刷新)
        pathgo(state) {
            if (state.path) return state.path;
            let path = localStorage.getItem("mypath");
            state.path = path;
            return state.path;
        },
        // 浏览器刷新,保留用户数据
        myuser(state) {
            if (state.loginuser) return state.loginuser;
            let loginuser = localStorage.getItem("user");
            state.loginuser = JSON.parse(loginuser);
            return state.loginuser;
        },
        //浏览器刷新,拿到存储地址
        mycity(state) {
            if (state.sitegood.length > 0) return state.sitegood;
            let sitess = localStorage.getItem("address");
            if (!sitess) return [];
            state.sitegood = JSON.parse(sitess);
            return state.sitegood;
        },
    },
    mutations: {
        //存储token
        getToken(state, payload) {
            state.token = payload;
            localStorage.setItem("token", payload);
        },
        // 存储跳转的路径
        mypath(state, payload) {
            state.path = payload;
            console.log(123);
        },
        // 存储用户信息
        setloguser(state, payload) {
            state.loginuser = payload;
            // 存储用户资料
            localStorage.setItem("user", JSON.stringify(payload));
        },
        // 退出登录,清空VueX 跟 localStorage
        loginout(state) {
            state.token = null;
            state.path = null;
            state.loginuser = null;
            localStorage.removeItem("mypath");
            localStorage.removeItem("token");
            localStorage.removeItem("user");
        },
        // 存储购物车信息
        getCat(state, payload) {
            state.catList.unshift(payload);
        },
        // 删除一条购物车的信息
        del(state, payload) {
            state.catList.splice(payload, 1);
        },
        // 在购物车增加商品数量
        addcat(state, payload) {
            state.catList.forEach((item) => {
                if (item.id == payload) {
                    item.count++;
                }
            });
        },
        // 在购物车减少商品数量
        ajjcat(state, payload) {
            state.catList.forEach((item) => {
                if (item.id == payload) {
                    item.count--;
                }
            });
        },
        // 改变商品的选中状态
        changecat(state, payload) {
            state.catList.forEach((item) => {
                if (item.id == payload) {
                    item.check = !item.check;
                }
            });
        },
        // 全部选中功能
        allcat(state) {
            state.catList.map((item) => {
                item.check = true;
            });
        },
        // 全部不选功能
        noallcat(state) {
            state.catList.map((item) => {
                item.check = false;
            });
        },
        // 删除商品
        removecay(state) {
            state.catList = state.catList.filter((item) => {
                return !item.check;
            });
        },
        // 保存新增地址
        setcity(state, payload) {
            state.sitegood.unshift(payload);
            state.sitegood = _.uniq(state.sitegood);
        },
        // 删除一个地址地址
        removeOneCity(state, payload) {
            state.sitegood.splice(payload, 1);
        },
        // 改变地址默认选中状态
        changecity(state, payload) {
            // state.sitegood[payload].isDefault = true;
            state.sitegood.forEach((item) => {
                if (item.id == payload) {
                    item.isDefault = true;
                } else {
                    item.isDefault = false;
                }
            });
        },
        // 添加一条数据到代发货
        drop(state, payload) {
            state.dropShipping.unshift(payload);
        },
        // 改变金额
        changemoney(state, payload) {
            state.money += payload;
        },
        // 减少金额
        reduce(state, payload) {
            state.money -= payload;
        },
        // 上传文件,获取用户图片数据
        changePath(getters, payload) {
            getters.myuser.avatar = payload;
        },
        // 修改loding的状态
        changeLoding(state, payload) {
            state.lodigChange = payload;
            console.log("改变状态");
            console.log(payload);
        },
    },
    actions: {
        // 获取用户信息
        getUserInfo(context) {
            myaxios.get("/user").then((re) => {
                console.log("用户信息", re);
                context.commit("setloguser", re);
            });
        },
    },
    modules: {},
});
